package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.userdata.ChatBuddy;
import com.motu.monstercity.server.game.userdata.ChatPublic;
import com.motu.monstercity.server.game.userdata.ChatUnion;
import com.motu.vertx.module.utility.model.EntityManager;
import com.motu.vertx.module.utility.model.Model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ChatUserData extends ChatBaseData {
//    public static ChatPublic getChatPublic(int groupId, long dbId) {
//        return (ChatPublic) EntityManager.getEntity(ChatPublic.class, groupId, dbId);
//    }

    public static List<ChatPublic> getChatPublicList(int groupId) {
        List<Model> list = EntityManager.getEntityList(ChatPublic.class, groupId);
        List<ChatPublic> chatPublicList = new ArrayList<>();
        if (list == null || list.isEmpty()) {
            return chatPublicList;
        }
        for (Model model : list) {
            ChatPublic chatPublic = (ChatPublic) model;
            chatPublicList.add(chatPublic);
        }
        Collections.sort(chatPublicList, new Comparator<ChatPublic>() {
            @Override
            public int compare(ChatPublic o1, ChatPublic o2) {
                return o1.getId() > o2.getId() ? -1 : 1;
            }
        });
        return chatPublicList;
    }

    public static List<ChatPublic> getChatPublicList(int groupId, int subType) {
        List<Model> list = EntityManager.getEntityList(ChatPublic.class, groupId);
        List<ChatPublic> chatPublicList = new ArrayList<>();
        if (list == null || list.isEmpty()) {
            return chatPublicList;
        }
        for (Model model : list) {
            ChatPublic chatPublic = (ChatPublic) model;
            if (chatPublic.getSubType() != subType) {
                continue;
            }
            chatPublicList.add(chatPublic);
        }
        Collections.sort(chatPublicList, new Comparator<ChatPublic>() {
            @Override
            public int compare(ChatPublic o1, ChatPublic o2) {
                return o1.getId() > o2.getId() ? -1 : 1;
            }
        });
        return chatPublicList;
    }

//    public static ChatUnion getChatUnion(int groupId, long dbId) {
//        return (ChatUnion) EntityManager.getEntity(ChatUnion.class, groupId, dbId);
//    }

    public static List<ChatUnion> getChatUnionList(long unionId) {
        List<Model> list = EntityManager.getEntityList(ChatUnion.class, unionId);
        List<ChatUnion> chatUnionList = new ArrayList<>();
        if (list == null || list.isEmpty()) {
            return chatUnionList;
        }
        for (Model model : list) {
            ChatUnion chatUnion = (ChatUnion) model;
            chatUnionList.add(chatUnion);
        }
        Collections.sort(chatUnionList, new Comparator<ChatUnion>() {
            @Override
            public int compare(ChatUnion o1, ChatUnion o2) {
                return o1.getId() > o2.getId() ? -1 : 1;
            }
        });
        return chatUnionList;
    }

    public static ChatBuddy getChatBuddy( long dbId) {
        return (ChatBuddy) EntityManager.getEntity(ChatBuddy.class, dbId);
    }

    public static List<ChatBuddy> getChatBuddyList(long userId1,long userId2) {
        List<Model> list = EntityManager.getEntityList(ChatBuddy.class, ChatManager.getUserJointId(userId1,userId2));
        List<ChatBuddy> chatBuddyList = new ArrayList<>();
        if (list == null || list.isEmpty()) {
            return chatBuddyList;
        }
        for (Model model : list) {
            ChatBuddy chatBuddy = (ChatBuddy) model;
            chatBuddyList.add(chatBuddy);
        }
        Collections.sort(chatBuddyList, new Comparator<ChatBuddy>() {
            @Override
            public int compare(ChatBuddy o1, ChatBuddy o2) {
                return o1.getId() > o2.getId() ? -1 : 1;
            }
        });
        return chatBuddyList;
    }

}
